import { reactive, watch, ref } from 'vue'
import { removeUndefinedParams } from '@/utils/index'
import type { BasicPagingListParams } from '@/apis/basicModel'
import { relationFactorList } from '@/apis/pcf/myModel/modelDetail'
import type { IFactorListParams } from '@/apis/pcf/myModel/model/modelDetailModel'
export const useFactorList = () => {
  const searchForm = reactive({
    factorNameOrNo: undefined,
    category: undefined,
    factorSource: undefined,
    country: undefined,
    tag: undefined
  })
  const tableRef = ref()
  function loadData(parameter: BasicPagingListParams) {
    const params = Object.assign(handleParams(), parameter)
    return relationFactorList(params)
  }

  // 处理参数
  const handleParams = () => {
    // 参数
    let params: IFactorListParams = {
      factorNameOrNo: searchForm.factorNameOrNo,
      category: searchForm.category,
      country: searchForm.country,
      factorSource: searchForm.factorSource,
      tag: searchForm.tag
    }
    params = removeUndefinedParams(params)
    return params
  }
  watch(searchForm, () => {
    tableRef.value?.refresh(true)
  })
  return {
    searchForm,
    loadData,
    tableRef
  }
}
